import { Injectable, OnApplicationShutdown } from "@nestjs/common";

@Injectable()
export class ShutdownService implements OnApplicationShutdown {
  // 1. 接收终止信号后，先执行 beforeShutdown(核心倒计时逻辑)
  async beforeShutdown(signal: string): Promise<number> {
    console.info(`Received shutdown signal: ${signal}`);
    return 15000;
  }

  // 2. 倒计时完成后，执行 onApplicationShutdown(最终清理)
  async onApplicationShutdown(signal: string) {
     console.log('Server will be shut down... within 10 seconds.');
    for (let i = 10; i > 0; i--) {
      console.log(`Server will be shut down in ${i} seconds.`);
      await new Promise(resolve => setTimeout(resolve, 1000));
    }
    console.log(`Server has been shut down(信号：${signal})`);
  }
}